package com.example.smsservice;

import java.util.ArrayList;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import com.example.smsservice.SmsInfo;

public class SmsReceiver extends BroadcastReceiver {
	static ArrayList<SmsInfo> listSms = new ArrayList<SmsInfo>();
	@Override
	public void onReceive(Context context, Intent intent) {
		// get SMS map from intent
        Bundle extras = intent.getExtras();
        // a notification message
        String messages = "";
        String _validate="";
        Bundle bundle = new Bundle();	
        if ( extras != null ) {
            // get array data from SMS
            Object[] smsExtra = (Object[]) extras.get( "pdus" ); // "pdus" is the key

            for ( int i = 0; i < smsExtra.length; ++i ) {
            	// get sms message
            	SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
            	// get content and number
            	String body = sms.getMessageBody();
            	String address = sms.getOriginatingAddress();
            	//String TKTDetails = "";
            	// create display message
            	messages += "SMS from " + address + " :\n";
            	messages += body + "\n";
            	
            	String str = body;
            	String delimiter = ",";
            	String[] temp;
            	  temp = str.split(delimiter);
            	  for(int k =0; k < temp.length ; k++)
            	  {
            	    if(k==0)
            	    {
            	    	//String delimiter1 = ":";
                    	//String[] temp1;
                    	 // temp1 = temp[k].split(delimiter1);
            	    	bundle.putString("TKTid", temp[k]);
            	    }
            	    if(k==1)
            	    {
            	    	bundle.putString("NOC_TKT_ID", temp[k]);
            	    }
            	    if(k==2)
            	    {
            	    	bundle.putString("Completion_Status", temp[k]);
            	    }
            	    if(k==3)
            	    {
            	    	bundle.putString("IMEI", temp[k]);
            	    }
            	    if(k==4)
            	    {
            	    	bundle.putString("wmac", temp[k]);
            	    }
            	    if(k==5)
            	    {
            	    	bundle.putString("SID", temp[k]);
            	    }
            	    if(k==6)
            	    {
            	    	
            	    	_validate=temp[k].toString();
            	    }
            	   /* if(k==6)
            	    {
            	    	TKTDetails=temp[k].toString();
            	    	
            	    }*/
            	  }
            	
            	// store in the list
                
            	  //listSms.add(new SmsInfo(address, body));
            	  
            	  listSms.add(new SmsInfo(address, body));
            }

            // better check size before continue
            if(listSms.size() > 0) {
            	// notify new arriving message
            	if(_validate.equals("validate"))
            	{
            		Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
	            	// set data to send
		            Intent data = new Intent(context, validateServiceActivity.class);
		            // new activity
		            data.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	
		            data.putParcelableArrayListExtra("ListSMS", listSms);
		            // start
		            data.putExtras(bundle);
		            context.startActivity(data);
            	}
            	else
            	{
				
	            	Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
	            	// set data to send
		            Intent data = new Intent(context, SMSReceiverServiceActivity.class);
		            // new activity
		            data.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	
		            data.putParcelableArrayListExtra("ListSMS", listSms);
		            // start
		            data.putExtras(bundle);
		            context.startActivity(data);
            	}
            }
        }
    }
}

